import Vue from 'vue'
import VueRouter from 'vue-router'
//使用路由懒加载
const Login=()=>import("../components/Login")
// import Login from "@/components/Login";
const Home=()=>import("@/components/Home")
// import Home from "@/components/Home";
const Welcome=()=>import( "../components/Welcome")
// import Welcome from "../components/Welcome";
const User=()=>import("../components/user/User")
// import User from "../components/user/User";
const Rights=()=>import("../components/power/Rights")
// import Rights from "../components/power/Rights";
const Roles=()=>import("../components/power/Roles")
// import Roles from "../components/power/Roles";
const Cate=()=>import("../components/goods/Cate")
// import Cate from "../components/goods/Cate";
const Params=()=>import("../components/goods/Params")
// import Params from "../components/goods/Params";
const List=()=>import("../components/goods/List")
// import List from "../components/goods/List";
const Add=()=>import("../components/goods/Add")
// import Add from "../components/goods/Add";
const Order=()=>import("../components/order/Order")
// import Order from "../components/order/Order";
const Report=()=>import("../components/report/Report")
// import Report from "../components/report/Report";
const NotFound=()=>import("../components/NotFound")
Vue.use(VueRouter)

const routes = [
  //没有任何一个匹配时就跳转到404
  {path: '*',redirect: '/404'},
  {path: '/404', component: NotFound },
  {path: '/', redirect:'/login'},
  {path:'/login', component:Login},
  {
    path:'/home',
    component:Home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:User},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/goods',component:List},
      {path:'/goods/add',component:Add},
      {path:'/orders',component:Order},
      {path:'/reports',component:Report}
    ]
  }
]

const router = new VueRouter({
  mode:'history',
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
//  to 将要访问的路径
//  from 代表从哪个路径跳转过来
//  next 是一个函数 表示放行
//  next() 放行 next('/login') 强制跳转

  if(to.path==='/login') return next();
//  获取token
  const tokenStr=window.sessionStorage.getItem('token')
  if (!tokenStr) next('/login')
  next()
})

export default router
